<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/9/2
 * Time: 14:31
 */

namespace addons\TfFutures\api\controllers;

use addons\TfFutures\common\models\LossSet;
use addons\TfFutures\common\models\Order;
use Yii;
use common\helpers\ResultDataHelper;
use api\controllers\OnAuthController;
use addons\TfFutures\common\models\Symbol;

class LossSetController extends OnAuthController
{
    public $modelClass= LossSet::class;
    /**
     * 止盈止损列表
     */
    public function actionLosslist()
    {
        if (!Yii::$app->request->isGet) {
            return ResultDataHelper::api(304, 'access done!');
        }
        $uid = Yii::$app->user->identity->member_id;
        $list = LossSet::find()
            ->where(['member_id' => $uid])
            ->andWhere(['<>','status','-1'])
            ->orderBy('create_at desc')
            ->all();
//        echo '<pre>';
//        var_dump($list);
//        exit;
        if (empty($list)) {
            return ResultDataHelper::api(422, '暂无数据');
        }
        return ResultDataHelper::api(200, '成功', $list);
    }

    /**
     * 止盈损添加
     */
    public function actionLossAdd()
    {
        if (!Yii::$app->request->isPost) {
            return ResultDataHelper::api(304, 'access done!');
        }
        $postdate = Yii::$app->request->post();
        $symbol = Symbol::find()->where(['code'=>$postdate['symbol']])->one();
        $loss = LossSet::find()->where(['symbol',$postdate['symbol']])->one();
        if(empty($symbol)){
            return ResultDataHelper::api(422, '产品未查到');
        }
        if (!empty($loss)){
            return ResultDataHelper::api(422, '该产品已设置过止盈损');
        }
        $LossSet = new LossSet();
        $uid = Yii::$app->user->identity->member_id;
        $LossSet->member_id = $uid;
        $LossSet->symbol = $postdate['symbol'];
        $LossSet->status = $postdate['status'];
        $LossSet->stop_profit_point = $postdate['stop_profit_point'];
        $LossSet->stop_loss_point = $postdate['stop_loss_point'];
        $LossSet->create_at = time();
        $LossSet->fall_rise = $postdate['fall_rise'];
        $LossSet->status = $postdate['status'];
        if(empty($postdate['status'])){
            $LossSet->status ='0';
        }
        if (!$LossSet->save()) {
            return ResultDataHelper::api(422, '添加失败');
        }
        return ResultDataHelper::api(200, '添加成功');
    }

    /**
     * 止盈损修改
     */
    public function actionLossUpdate()
    {
        if (!Yii::$app->request->isPost) {
            return ResultDataHelper::api(304, 'access done!');
        }
        $postdate = Yii::$app->request->post();
        $LossSet = new LossSet();
        $LossSet = $LossSet::find()->where(['id' => Yii::$app->request->post('id')])->one();
        if(empty($LossSet)){
            return ResultDataHelper::api(422, '暂无数据!');
        }

//        $LossSet->member_id = $uid;
        $LossSet->symbol = $postdate['symbol'];
        $LossSet->stop_profit_point = $postdate['stop_profit_point'];
        $LossSet->stop_loss_point = $postdate['stop_loss_point'];
        $LossSet->fall_rise = $postdate['fall_rise'];
        $LossSet->status = $postdate['status'];
        $symbol = Symbol::find()->where(['code'=>$postdate['symbol']])->one();
        $order = Order::find()->where(['symbol'=>$symbol->code2,'member_id'=>$LossSet->member_id])->all();
//        Yii::$app->db->createCommand("UPDATE `rf_addon_tf_futures_order` SET `symbol` = {$symbol->code2} ,  `member_id` = {$price} WHERE `symbol` = '{$symbolid}' AND `state` = '持仓' AND `status` = 1")->execute();
        foreach ($order as $k =>$v){
            if($v->rise_fall=='RISE'){
                $v['stop_profit_point'] = $LossSet->stop_profit_point;
                $v['stop_loss_point'] = $LossSet->stop_loss_point;
            }else{
                $v['stop_profit_point'] = $LossSet->stop_loss_point;
                $v['stop_loss_point'] = $LossSet->stop_profit_point;
            }
            $v->save();
        }
        if (!$LossSet->save()) {
            return ResultDataHelper::api(422, '修改失败');
        }
        return ResultDataHelper::api(200, '修改成功');
    }
    /**
     * 止盈损设置删除
     */
    public function actionLossDelete(){
        if (!Yii::$app->request->isGet) {
            return ResultDataHelper::api(304, 'access done!');
        }
        $id = Yii::$app->request->get('id');
        if(empty($id)){
        	return ResultDataHelper::api(422, 'ID不能为空!');
        }
        $LossSet = new LossSet();
        $loss = $LossSet::find()->where(['id' => $id])->one();
        $loss->status = -1;
        if(!$loss->save()){
            return ResultDataHelper::api(422, '删除失败');
        }
        return ResultDataHelper::api(200, '删除成功');
    }
    /**
     * 列表查看
     */
    public function actionLossCheck(){
        if (!Yii::$app->request->isGet) {
            return ResultDataHelper::api(304, 'access done!');
        }
        $LossSet = new LossSet();
        $loss = $LossSet::find()->where(['id' => Yii::$app->request->get('id')])->one();
        if(!empty($loss)){
            return ResultDataHelper::api(200, '查询成功',$loss);
        }else{
            return ResultDataHelper::api(422, '暂无数据');
        }
    }
}